package com.leadingsoft.pwxk.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.leadingsoft.common.dto.TableResultDto;
import com.leadingsoft.pwxk.converter.AnnouncementConverter;
import com.leadingsoft.pwxk.dto.AnnouncementDto;
import com.leadingsoft.pwxk.dto.SearchDto;
import com.leadingsoft.pwxk.model.Announcement;
import com.leadingsoft.pwxk.service.AnnouncementService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author ysh
 * @desc 通知公告
 */
@Api(description = "通知公告")
@RequestMapping("api/announcement")
@RestController
public class AnnouncementController {

	@Autowired
	AnnouncementService announcementService;
	@Autowired
	AnnouncementConverter announcementConverter;

	@ApiOperation("新增")
	@PostMapping
	@ResponseStatus(code = HttpStatus.OK)
	public AnnouncementDto save(@RequestBody AnnouncementDto announcementDto) {
		return announcementConverter.toDto(announcementService.save(announcementDto));
	}

	@ApiOperation("更新")
	@PutMapping(path = "{id}")
	@ResponseStatus(code = HttpStatus.OK)
	public AnnouncementDto update(@RequestBody AnnouncementDto announcementDto) {
		return announcementConverter.toDto(announcementService.update(announcementDto));
	}

	@ApiOperation("删除")
	@DeleteMapping(path = "{id}")
	@ResponseStatus(code = HttpStatus.OK)
	public void delete(@PathVariable Long id) {
		announcementService.delete(id);
	}

	@ApiOperation("查询详情")
	@GetMapping(path = "{id}")
	@ResponseStatus(code = HttpStatus.OK)
	public AnnouncementDto get(@PathVariable Long id) {
		return announcementConverter.toDto(announcementService.get(id));
	}

	@ApiOperation("获取列表")
	@GetMapping(params = { "draw" })
	@ResponseStatus(code = HttpStatus.OK)
	public TableResultDto<AnnouncementDto> getList(
			@PageableDefault(page = 0, size = 10, sort = {
					"updateTime" }, direction = Direction.DESC) Pageable pageable,
			@RequestParam(value = "draw", required = false) Long draw, SearchDto s) {
		Page<Announcement> list = announcementService.getList(s, pageable);
		return TableResultDto.success(draw, list, announcementConverter);
	}

	@ApiOperation("获取前7")
	@GetMapping("top")
	@ResponseStatus(code = HttpStatus.OK)
	public List<AnnouncementDto> getTop7() {
		return announcementConverter.toDto(announcementService.getTop7());
	}
}
